import { request, useUserStore, clearEmpty } from "jmash-core";
import type { AxiosPromise } from "axios";
import type {
  JobApi,
  JobReq,
  JobPage,
  JobUpdateReq,
  JobCreateReq,
  JobKeyList,
  JobMoveKey,
  JobKey,
  JobModel,
} from "./types";
import type { LayEntryList } from "@/api/system/types";

class JobApiImpl implements JobApi {
  getKey(model: JobModel): JobKey {
    return {
      roleId: model.roleId,
    } as JobKey;
  }
  /**
   * 获取分页数据
   *
   * @param queryParams 查询参数，可选
   * @returns 返回请求结果
   */
  findPage(query: JobReq): AxiosPromise<JobPage> {
    query.tenant = useUserStore().user.tenant;
    if (query) {
      query.hasRoleType = query.roleType != undefined;
    }
    clearEmpty(query);
    return request({
      url: "/v1/rbac/role/list",
      method: "get",
      params: query,
    });
  }
  // 查询角色/职务表
  findById(query: JobKey): AxiosPromise<JobModel> {
    query.tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/rbac/role/id",
      method: "get",
      params: query,
    });
  }
  /**
   * 编辑职位
   *
   * @param data 编辑数据
   * @returns 返回请求结果
   */
  update(data: JobUpdateReq): AxiosPromise<JobModel> {
    data.tenant = useUserStore().user.tenant as string;
    clearEmpty(data);
    return request({
      url: "/v1/rbac/role",
      method: "patch",
      data: data,
    });
  }

  /**
   * 职位新增
   *
   * @param data 任务数据
   * @returns 返回请求结果
   */
  create(data: JobCreateReq): AxiosPromise<JobModel> {
    data.tenant = useUserStore().user.tenant;
    clearEmpty(data);
    return request({
      url: "/v1/rbac/role",
      method: "post",
      data: data,
    });
  }
  /**
   * 职位删除
   *
   * @param roleId 角色ID
   * @returns 返回请求结果
   */
  delete(query: JobKey): AxiosPromise<JobModel> {
    query.tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/rbac/role/id",
      method: "delete",
      params: query,
    });
  }
  /**
   * 岗位批量删除
   *
   * @param ids 任务ID数组
   * @returns 返回请求结果
   */
  batchDelete(keys: JobKey[]): AxiosPromise<number> {
    const ids = keys.map((item) => item.roleId);
    const data: JobKeyList = {
      tenant: useUserStore().user.tenant,
      roleId: ids,
    } as JobKeyList;
    clearEmpty(data);
    return request({
      url: "/v1/rbac/role/batch",
      method: "put",
      data: data,
    });
  }
  /**
   * 向上/下移动部门
   *
   * @param roleId 部门ID
   * @returns 返回Promise对象，表示异步操作的结果
   */
  move(key: JobKey, up: boolean): AxiosPromise<boolean> {
    const data: JobMoveKey = { ...key };
    data.tenant = useUserStore().user.tenant;
    data.up = up;
    clearEmpty(data);
    return request({
      url: "/v1/rbac/role/move",
      method: "put",
      data: data,
    });
  }

  /**
   * 获取树形列表中所有岗位信息
   *
   * @returns 返回请求结果
   */
  treelistJob(): AxiosPromise<LayEntryList> {
    const query = {
      tenant: useUserStore().user.tenant,
      roleType: "job",
      hasRoleType: true,
    };
    return request({
      url: "/v1/rbac/role/treelist",
      method: "get",
      params: query,
    });
  }
}
const jobApi = new JobApiImpl();
export { jobApi };
